const db = uniCloud.database();
let accessKey = "57e2fd8631744779be404921c23e6532"
let secretKey = "fce09d192e05429cbd3a34b54a234ede"
const crypto = require("crypto");

function getSign(params) {
	params.secretKey = secretKey
	let str = [params.orderNo, params.status, params.submitTime, params.timestamp, params.accessKey, params.secretKey]
		.sort().join('')
	let sign = crypto.createHash('md5').update(str, 'utf8').digest('hex');
	return sign
}
module.exports = {
	_before: function() {

	},
	async callback(params) {
		let data = {
			orderNo: params.orderNo,
			status: 2,
			submitTime: new Date().getTime(),
			accessKey: accessKey,
			timestamp: new Date().getTime()
		}
		data.sign = getSign(data)
		data.ext = {
			phone: params.phone,
			price: params.price
		}
		let requestUrl = 'https://actengine.ydns.cn/open/advertiser/callback';
		return await uniCloud.httpclient.request(requestUrl, {
			headers: {
				"content-type": "application/json",
			},
			method: 'POST',
			data: data,
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		});
	}
}